/*
 * @Description:
 * @Author: dh
 * @Date: 2022-01-21 13:41:19
 * @LastEditors: dh
 * @LastEditTime: 2022-07-18 10:36:43
 */
import { reactive, onMounted, onUnmounted, toRefs } from 'vue'

export default function useMouse() {
	const state = reactive({
		x: 0,
		y: 0,
	})
	const update = (e: any) => {
		state.x = e.pageX
		state.y = e.pageY
	}
	onMounted(() => {
		window.addEventListener('mousemove', update)
	})
	onUnmounted(() => {
		window.removeEventListener('mousemove', update)
	})

	// 这里导出的是ref对象，需要 .value 获取值 和 获取类型推导
	return toRefs(state)
	// or
	// return {...toRefs(state)};
}
